﻿using AutoMapper;
using Morin.Services;
using Morin.Shared.Models;
using Morin.Shared.Parameters;
using Morin.Wpf.Adapters;
using Morin.Wpf.Common;
using Morin.Wpf.ViewModels.Players;
using Morin.Wpf.Views.Account;
using Stylet;
using StyletIoC;
using System.Windows.Forms.Design;

namespace Morin.Wpf.ViewModels.Account;

internal class FavoriteViewModel(
    IContainer container,
    ISourceProtocolAdapter sourceProtocolAdapter,
    IMapper mapper,
    IApiService apiService,
    IAppService appService,
    IWindowManager windowManager) : Screen
{


    #region 分页属性

    public int Total { get; set; }
    public int PageSize { get; set; } = 20;
    private int _pageIndex = 1;

    public int PageIndex
    {
        get => _pageIndex;
        set
        {
            _pageIndex = value;
            PageIndexChangedAsync(value);
        }
    }

    #endregion

    private IEnumerable<FavoriteModel>? _favoriteEnumerable;
    public BindableCollection<FavoriteModel>? Favorites { get; set; }

    protected override void OnInitialActivate()
    {
        base.OnInitialActivate();
        _favoriteEnumerable = appService?.GetFavorites().ToList();
        if (_favoriteEnumerable != null)
        {
            //  赋值来源名称
            foreach (var item in _favoriteEnumerable)
                if (string.IsNullOrEmpty(item.VodSourceTitle))
                {
                    var source = appService?.GetMediaSources().FirstOrDefault(x => x.Id == item.VodSourceID);
                    item.VodSourceTitle = source?.Title;
                }

            //  按观看时间倒序排列
            _favoriteEnumerable = _favoriteEnumerable.OrderByDescending(x => x.ViewTime);
            Total = _favoriteEnumerable.Count();
        }

        PageIndexChangedAsync(1);
    }

    public Task PlayAsync(FavoriteModel o)
    {
        return Task.Run(async () =>
        {
            var req = new ReqQryVideoDetailPara { SourceId = o.VodSourceID, VodIds = $"{o.VodId}", AcName = "detail" };
            var detail = await apiService?.ReqQryVideoDetailsAsync(req)!;
            await Execute.PostToUIThreadAsync(() =>
            {
                var playVm = container?.Get<PlayerViewModel>();

                //  播放列表
                var para = mapper.Map<ThinkPhpVideoParsingPara>(detail);
                if (playVm == null) return;
                playVm.PlayDict = sourceProtocolAdapter.GetPlayDict(para);
                windowManager?.ShowWindow(playVm);
            });
        });
    }

    private Task PageIndexChangedAsync(int pageIndex)
    {
        return Task.Run(() =>
        {
            if (_favoriteEnumerable != null)
                Execute.PostToUIThreadAsync(() =>
                {
                    if (PageIndex == 1)
                        Favorites = [.. _favoriteEnumerable.Take(PageSize)];
                    else
                        Favorites = [.. _favoriteEnumerable.Skip((PageIndex - 1) * PageSize).Take(PageSize)];
                });
        });
    }
}